package com.wythe.mall.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.google.gson.Gson;
import com.hb.dialog.myDialog.MyAlertDialog;
import com.wythe.mall.R;
import com.wythe.mall.adapter.GoodsOrderListAdapter;
import com.wythe.mall.base.BaseActivity;
import com.wythe.mall.intf.OnRequestDataListener;
import com.wythe.mall.model.OrderBean;
import com.wythe.mall.utils.ApiOkHttp;
import com.wythe.mall.utils.GotoActivity;
import com.wythe.mall.utils.TimeUtil;
import com.wythe.mall.utils.ToastUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class OrderInfoActivity extends BaseActivity implements View.OnClickListener {
    private String orderNo;
    private List<OrderBean.ItemVoBean> itemVoBeans;
    private GoodsOrderListAdapter mAdapter;
    private RecyclerView mainRecyclerView;
    private TextView mTvDes, mName, mAddress, mCount, mPrice, mPayPrice, mOrderNo, mTime, mCancel, mConfirm;
    private int status,type;
    private double mTotalPrice = 0;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            orderNo = extras.getString("orderNo");
        }
        setContentView(R.layout.activity_order_info);
        if (itemVoBeans == null) {
            itemVoBeans = new ArrayList<>();
        }
        itemVoBeans.clear();
        initView();
    }

    public void initView() {
        findViewById(R.id.title_left_button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        ((TextView) findViewById(R.id.titleText)).setText("订单详情");
        mTvDes = (TextView) findViewById(R.id.tv_des);
        mName = (TextView) findViewById(R.id.tv_name);
        mAddress = (TextView) findViewById(R.id.tv_address);
        mCount = (TextView) findViewById(R.id.tv_count);
        mPrice = (TextView) findViewById(R.id.tv_price);
        mPayPrice = (TextView) findViewById(R.id.tv_pay);
        mOrderNo = (TextView) findViewById(R.id.tv_orderNo);
        mTime = (TextView) findViewById(R.id.tv_time);
        mCancel = (TextView) findViewById(R.id.tv_cancel);
        mConfirm = (TextView) findViewById(R.id.tv_confirm);
        mConfirm.setOnClickListener(this);
        mCancel.setOnClickListener(this);
        mainRecyclerView = (RecyclerView) findViewById(R.id.mainRecyclerView);
        if (mAdapter == null) {
            mAdapter = new GoodsOrderListAdapter(this, itemVoBeans);
        }
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mainRecyclerView.setLayoutManager(linearLayoutManager);//设置布局管理器
        mainRecyclerView.setAdapter(mAdapter);
    }

    public void initData() {
        JSONObject param = new JSONObject();
        try {
            param.put("orderNo", orderNo);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        ApiOkHttp.queryOrderInfo(this, param, new OnRequestDataListener() {
            @Override
            public void requestSuccess(int code, JSONObject data) {
                itemVoBeans.clear();
                try {
                    JSONObject jsonObject = data.getJSONObject("data");
                    status = jsonObject.getInt("status");
                    type = jsonObject.getInt("type");
                    JSONArray itemVo = jsonObject.getJSONArray("itemVo");
                    for (int i = 0; i < itemVo.length(); i++) {
                        OrderBean.ItemVoBean bean = new Gson().fromJson(itemVo.getJSONObject(i).toString(), OrderBean.ItemVoBean.class);
                        itemVoBeans.add(bean);
                    }
                    mAdapter.notifyDataSetChanged();
                    String des = getTopDes(status, type, jsonObject);
                    mTvDes.setText(des);
                    mCount.setText(jsonObject.getInt("allCount") + "件");
                    mTotalPrice = jsonObject.getDouble("totalAmount");
                    mPrice.setText("￥"+ mTotalPrice);
                    mPayPrice.setText("￥"+mTotalPrice);

                    mOrderNo.setText("订单编号：" + jsonObject.getString("orderNo"));
                    mTime.setText("下单时间：" + TimeUtil.msToDate(jsonObject.getLong("timeStamp"), "yyyy-MM-dd HH:mm:ss"));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void requestFailure(int code, String msg) {

            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        initData();
    }

    private String getTopDes(int status, int type, JSONObject jsonObject) {
        String title = "";
        String name = "";
        String address = "";
        try {
            String code = jsonObject.getString("pickNo");
            switch (status) {
                case 1:
                    title = "您有订单待支付!";
                    if (type == 1) {//自提
                        name = jsonObject.getJSONObject("settlementVo").getString("title");
                        address = jsonObject.getJSONObject("settlementVo").getString("province") + " " +
                                jsonObject.getJSONObject("settlementVo").getString("city") + " " +
                                jsonObject.getJSONObject("settlementVo").getString("districe") + " " +
                                jsonObject.getJSONObject("settlementVo").getString("address");
                        mName.setText("自提店： " + name);
                        mAddress.setText("门店地址： " + address);

                    } else {//配送
                        name = jsonObject.getJSONObject("addressVo").getString("name");
                        address = jsonObject.getJSONObject("addressVo").getString("address");
                        mName.setText("收货人： " + name);
                        mAddress.setText("收货地址： " + address);
                    }

                    mCancel.setVisibility(View.VISIBLE);
                    mConfirm.setText("付款");
                    mConfirm.setVisibility(View.VISIBLE);
                    break;
                case 2:
                    title = "订单交易关闭!";
                    if (type == 1) {//自提
                        name = jsonObject.getJSONObject("settlementVo").getString("title");
                        address = jsonObject.getJSONObject("settlementVo").getString("province") + " " +
                                jsonObject.getJSONObject("settlementVo").getString("city") + " " +
                                jsonObject.getJSONObject("settlementVo").getString("districe") + " " +
                                jsonObject.getJSONObject("settlementVo").getString("address");
                        mName.setText("自提店： " + name);
                        mAddress.setText("门店地址： " + address);

                    } else {//配送
                        name = jsonObject.getJSONObject("addressVo").getString("name");
                        address = jsonObject.getJSONObject("addressVo").getString("address");
                        mName.setText("收货人： " + name);
                        mAddress.setText("收货地址： " + address);
                    }

                    mCancel.setVisibility(View.INVISIBLE);
                    mConfirm.setVisibility(View.INVISIBLE);
                    break;
                case 3:
                    title = "您的商品正在准备中，请稍等!";
                    if (type == 1) {//自提
                        name = jsonObject.getJSONObject("settlementVo").getString("title");
                        address = jsonObject.getJSONObject("settlementVo").getString("province") + " " +
                                jsonObject.getJSONObject("settlementVo").getString("city") + " " +
                                jsonObject.getJSONObject("settlementVo").getString("districe") + " " +
                                jsonObject.getJSONObject("settlementVo").getString("address");
                        mName.setText("自提店： " + name);
                        mAddress.setText("门店地址： " + address);

                    } else {//配送
                        name = jsonObject.getJSONObject("addressVo").getString("name");
                        address = jsonObject.getJSONObject("addressVo").getString("address");
                        mName.setText("收货人： " + name);
                        mAddress.setText("收货地址： " + address);
                    }

                    mCancel.setVisibility(View.INVISIBLE);
                    mConfirm.setVisibility(View.INVISIBLE);
                    break;
                case 5:
                    title = "配送中";
                    if (type == 1) {//自提
                        name = jsonObject.getJSONObject("settlementVo").getString("title");
                        address = jsonObject.getJSONObject("settlementVo").getString("province") + " " +
                                jsonObject.getJSONObject("settlementVo").getString("city") + " " +
                                jsonObject.getJSONObject("settlementVo").getString("districe") + " " +
                                jsonObject.getJSONObject("settlementVo").getString("address");
                        mName.setText("自提店： " + name);
                        mAddress.setText("门店地址： " + address);

                    } else {//配送
                        name = jsonObject.getJSONObject("addressVo").getString("name");
                        address = jsonObject.getJSONObject("addressVo").getString("address");
                        mName.setText("收货人： " + name);
                        mAddress.setText("收货地址： " + address);
                    }

                    mCancel.setVisibility(View.GONE);
                    mConfirm.setText("确认收货");
                    mConfirm.setVisibility(View.VISIBLE);
                    break;
                case 6://待收货
                    if (type == 1) {//自提
                        title = "您的商品已到达门店，请尽快提货，提货码: " + code;
                        name = jsonObject.getJSONObject("settlementVo").getString("title");
                        address = jsonObject.getJSONObject("settlementVo").getString("province") + " " +
                                jsonObject.getJSONObject("settlementVo").getString("city") + " " +
                                jsonObject.getJSONObject("settlementVo").getString("districe") + " " +
                                jsonObject.getJSONObject("settlementVo").getString("address");
                        mName.setText("自提店： " + name);
                        mAddress.setText("门店地址： " + address);

                    } else {//配送
                        title = "您的商品正在配送中，请稍等，配送码: " + code;
                        name = jsonObject.getJSONObject("addressVo").getString("name");
                        address = jsonObject.getJSONObject("addressVo").getString("address");
                        mName.setText("收货人： " + name);
                        mAddress.setText("收货地址： " + address);
                    }
                    mCancel.setVisibility(View.GONE);
                    mConfirm.setText("确认收货");
                    mConfirm.setVisibility(View.VISIBLE);
                    break;
                case 7:
                    title = "您有订单待评价!";
                    if (type == 1) {//自提
                        name = jsonObject.getJSONObject("settlementVo").getString("title");
                        address = jsonObject.getJSONObject("settlementVo").getString("province") + " " +
                                jsonObject.getJSONObject("settlementVo").getString("city") + " " +
                                jsonObject.getJSONObject("settlementVo").getString("districe") + " " +
                                jsonObject.getJSONObject("settlementVo").getString("address");
                        mName.setText("自提店： " + name);
                        mAddress.setText("门店地址： " + address);

                    } else {//配送
                        name = jsonObject.getJSONObject("addressVo").getString("name");
                        address = jsonObject.getJSONObject("addressVo").getString("address");
                        mName.setText("收货人： " + name);
                        mAddress.setText("收货地址： " + address);
                    }

                    mCancel.setVisibility(View.INVISIBLE);
                    mConfirm.setVisibility(View.INVISIBLE);
                    break;
                case 9:
                    title = "订单交易完成!";
                    if (type == 1) {//自提
                        name = jsonObject.getJSONObject("settlementVo").getString("title");
                        address = jsonObject.getJSONObject("settlementVo").getString("province") + " " +
                                jsonObject.getJSONObject("settlementVo").getString("city") + " " +
                                jsonObject.getJSONObject("settlementVo").getString("districe") + " " +
                                jsonObject.getJSONObject("settlementVo").getString("address");
                        mName.setText("自提店： " + name);
                        mAddress.setText("门店地址： " + address);

                    } else {//配送
                        name = jsonObject.getJSONObject("addressVo").getString("name");
                        address = jsonObject.getJSONObject("addressVo").getString("address");
                        mName.setText("收货人： " + name);
                        mAddress.setText("收货地址： " + address);
                    }

                    mCancel.setVisibility(View.INVISIBLE);
                    mConfirm.setVisibility(View.INVISIBLE);
                    break;
                case 13:
                    title = "您已申请退货!";
                    if (type == 1) {//自提
                        name = jsonObject.getJSONObject("settlementVo").getString("title");
                        address = jsonObject.getJSONObject("settlementVo").getString("province") + " " +
                                jsonObject.getJSONObject("settlementVo").getString("city") + " " +
                                jsonObject.getJSONObject("settlementVo").getString("districe") + " " +
                                jsonObject.getJSONObject("settlementVo").getString("address");
                        mName.setText("自提店： " + name);
                        mAddress.setText("门店地址： " + address);

                    } else {//配送
                        name = jsonObject.getJSONObject("addressVo").getString("name");
                        address = jsonObject.getJSONObject("addressVo").getString("address");
                        mName.setText("收货人： " + name);
                        mAddress.setText("收货地址： " + address);
                    }

                    mCancel.setVisibility(View.INVISIBLE);
                    mConfirm.setVisibility(View.INVISIBLE);
                    break;
                case 15:
                    title = "您的订单退货正在审核中!";
                    if (type == 1) {//自提
                        name = jsonObject.getJSONObject("settlementVo").getString("title");
                        address = jsonObject.getJSONObject("settlementVo").getString("province") + " " +
                                jsonObject.getJSONObject("settlementVo").getString("city") + " " +
                                jsonObject.getJSONObject("settlementVo").getString("districe") + " " +
                                jsonObject.getJSONObject("settlementVo").getString("address");
                        mName.setText("自提店： " + name);
                        mAddress.setText("门店地址： " + address);

                    } else {//配送
                        name = jsonObject.getJSONObject("addressVo").getString("name");
                        address = jsonObject.getJSONObject("addressVo").getString("address");
                        mName.setText("收货人： " + name);
                        mAddress.setText("收货地址： " + address);
                    }

                    mCancel.setVisibility(View.INVISIBLE);
                    mConfirm.setVisibility(View.INVISIBLE);
                    break;
                case 16:
                    if (type == 1) {
                        title = "您的订单退货已通过审核，请您退换到自提点!";
                        name = jsonObject.getJSONObject("settlementVo").getString("title");
                        address = jsonObject.getJSONObject("settlementVo").getString("province") + " " +
                                jsonObject.getJSONObject("settlementVo").getString("city") + " " +
                                jsonObject.getJSONObject("settlementVo").getString("districe") + " " +
                                jsonObject.getJSONObject("settlementVo").getString("address");
                        mName.setText("自提店： " + name);
                        mAddress.setText("门店地址： " + address);

                    } else {
                        title = "您的订单退货已通过审核，待配送员上门提取!";
                        name = jsonObject.getJSONObject("addressVo").getString("name");
                        address = jsonObject.getJSONObject("addressVo").getString("address");
                        mName.setText("收货人： " + name);
                        mAddress.setText("收货地址： " + address);
                    }

                    mCancel.setVisibility(View.INVISIBLE);
                    mConfirm.setVisibility(View.INVISIBLE);
                    break;
                case 17:
                    title = "订单退款中!";
                    if (type == 1) {//自提
                        name = jsonObject.getJSONObject("settlementVo").getString("title");
                        address = jsonObject.getJSONObject("settlementVo").getString("province") + " " +
                                jsonObject.getJSONObject("settlementVo").getString("city") + " " +
                                jsonObject.getJSONObject("settlementVo").getString("districe") + " " +
                                jsonObject.getJSONObject("settlementVo").getString("address");
                        mName.setText("自提店： " + name);
                        mAddress.setText("门店地址： " + address);

                    } else {//配送
                        name = jsonObject.getJSONObject("addressVo").getString("name");
                        address = jsonObject.getJSONObject("addressVo").getString("address");
                        mName.setText("收货人： " + name);
                        mAddress.setText("收货地址： " + address);
                    }

                    mCancel.setVisibility(View.INVISIBLE);
                    mConfirm.setVisibility(View.INVISIBLE);
                    break;
                case 19:
                    title = "订单交易关闭!";
                    if (type == 1) {//自提
                        name = jsonObject.getJSONObject("settlementVo").getString("title");
                        address = jsonObject.getJSONObject("settlementVo").getString("province") + " " +
                                jsonObject.getJSONObject("settlementVo").getString("city") + " " +
                                jsonObject.getJSONObject("settlementVo").getString("districe") + " " +
                                jsonObject.getJSONObject("settlementVo").getString("address");
                        mName.setText("自提店： " + name);
                        mAddress.setText("门店地址： " + address);

                    } else {//配送
                        name = jsonObject.getJSONObject("addressVo").getString("name");
                        address = jsonObject.getJSONObject("addressVo").getString("address");
                        mName.setText("收货人： " + name);
                        mAddress.setText("收货地址： " + address);
                    }

                    mCancel.setVisibility(View.INVISIBLE);
                    mConfirm.setVisibility(View.INVISIBLE);
                    break;
                default:
                    mCancel.setVisibility(View.INVISIBLE);
                    mConfirm.setVisibility(View.INVISIBLE);
                    break;
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }
        return title;
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        switch (id) {
            case R.id.tv_cancel:
                onCancel();
                break;
            case R.id.tv_confirm:
                if ("付款".equals(((TextView) view).getText())) {
                    Bundle bundle = new Bundle();
                    bundle.putString("orderNo", orderNo);
                    bundle.putDouble("payAmount", mTotalPrice);
                    GotoActivity.gotoActiviy(getActivity(), PayOrderActivity.class, bundle);
                } else if ("确认收货".equals(((TextView) view).getText())) {
                    MyAlertDialog myAlertDialog = new MyAlertDialog(getActivity());
                    myAlertDialog.builder();
                    myAlertDialog.setTitle("温馨提示");
                    myAlertDialog.setMsg("你是否确认收货？");
                    myAlertDialog.setPositiveButton("确认", new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            JSONObject param = new JSONObject();
                            try {
                                param.put("orderNo", orderNo);
                                param.put("status", 7);
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                            ApiOkHttp.editOrderList(getActivity(), param, new OnRequestDataListener() {
                                @Override
                                public void requestSuccess(int code, JSONObject data) {
                                    try {
                                        int code1 = data.getInt("code");
                                        if (code == 200) {
                                            initData();
                                        }
                                        ToastUtil.show(getActivity(), data.getString("msg"));
                                    } catch (JSONException e) {
                                        e.printStackTrace();
                                    }
                                }

                                @Override
                                public void requestFailure(int code, String msg) {

                                }
                            });
                        }
                    });
                    myAlertDialog.setNegativeButton("取消", new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                        }
                    });
                    myAlertDialog.show();
                }
                break;
        }
    }

    private void onCancel() {
        JSONObject param = new JSONObject();
        try {
            param.put("orderNo",orderNo);
            param.put("status",2);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        ApiOkHttp.editOrderList(getActivity(), param, new OnRequestDataListener() {
            @Override
            public void requestSuccess(int code, JSONObject data) {
                try {
                    int code1 = data.getInt("code");
                    if (code == 200) {
                        finish();
                    }
                    ToastUtil.show(getActivity(),data.getString("msg"));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void requestFailure(int code, String msg) {

            }
        });

    }
}
